<?php

/**
 * 指标公用方法
 * @author w.
 *
 */
class AlgFunc {
    
    // 移动平均SMA,(X,N,M)
    // Y=[M*X+(N-M)*Y']/N
    public static function getSma($price,$pricePrev,$length,$weight){
        $rst = ($weight * $price + $pricePrev*($length-$weight))/$length;
        return round($rst,2);
    }
    
    // N日内的最高价
    public static function highest($bars,$length,$shift,&$hIndex){
        $hRst = 0;
        if($shift == 0){
            $hIndex = 0;
            $hRst = $bars[0][HIGH];
            return $hRst;
        }
        $len = $shift-$length;
        if($len<0)$len = -1;
        for($i=$shift;$i>$len;$i--){
            if($bars[$i][HIGH] > $hRst){
                $hRst = $bars[$i][HIGH];
                $hIndex = $i;
            }
        }
        return $hRst;
    }
    
    // N日内的最低价
    public static function lowest($bars,$length,$shift,&$lIndex){
        $lRst = 9999;
        if($shift == 0){
            $lIndex = 0;
            $lRst = $bars[0][LOW];
            return $lRst;
        }
        $len = $shift-$length;
        if($len<0)$len = -1;
        for($i=$shift;$i>$len;$i--){
            if($bars[$i][LOW] < $lRst){
                $lRst = $bars[$i][LOW];
                $lIndex = $i;
            }
        }
        return $lRst;
    }

    // 获取涨幅
    public static function getRise($barIndex, $open, $close,$pre_close) {
		$rise = 0;
		if($barIndex!=0){
			if($pre_close == 0){
				$rise = round($close*100,2);		
			} else {
				$rise = round((($close-$pre_close)/$pre_close)*100,2);	
			}
		} else {
			if($open==0){
				$rise = round($close*100,2);
			} else {
				$rise = round((($close-$open)/$open)*100,2);
			}
		}
		return $rise;
	}
}

?>
